package com.taptech.doufu.ui.view.drawcircle;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.util.TagsUtil;

import java.util.List;


/**
 * Created by lsy on 2015/6/30.
 */
public class DoufuTagsAdapter extends RecyclerView.Adapter<DoufuTagsAdapter.DrawTagsViewHolder> implements View.OnClickListener
{

    private Context mContext;
    private List<String> mDataList;

    private String type;
    private int mTextSize = -1;

    private int mTotalWidth = 0;

    public DoufuTagsAdapter(Context context, List<String> dataList)
    {
        this.mContext = context;
        this.mDataList = dataList;
    }


    public DoufuTagsAdapter(Context context, List<String> dataList, String type)
    {
        this.mContext = context;
        this.mDataList = dataList;
        this.type = type;
    }

    @Override
    public DrawTagsViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view = LayoutInflater.from(mContext).inflate(R.layout.draw_single_tag_layout, parent, false);
        DrawTagsViewHolder viewHolder = new DrawTagsViewHolder(view);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(DrawTagsViewHolder holder, int position)
    {
        holder.tagContent.setText(mDataList.get(position).toString());

        if (mTextSize > 0)
        {
            holder.tagContent.setTextSize(mTextSize);
        }
        holder.tagContent.setOnClickListener(this);
        holder.itemView.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.UNSPECIFIED);
        mTotalWidth += holder.itemView.getMeasuredWidth();
    }

    @Override
    public int getItemCount()
    {
        if (mDataList != null)
        {
            return mDataList.size();
        }
        return 0;
    }

    @Override
    public void onClick(View view)
    {
        TextView tagTextView = (TextView) view;
        String tagContentString = tagTextView.getText().toString();
        if (type != null)
        {
            TagsUtil.enterTagSearchActivity(mContext, tagContentString, type);
        } else
        {
            TagsUtil.enterTagSearchActivity(mContext, tagContentString);
        }

    }

    public static class DrawTagsViewHolder extends RecyclerView.ViewHolder
    {

        public TextView tagContent;

        public DrawTagsViewHolder(View itemView)
        {
            super(itemView);
            tagContent = (TextView) itemView.findViewById(R.id.tag_content_text);
        }
    }


    public void setTagsTextSize(int textSize)
    {
        mTextSize = textSize;
    }


    public int getmTotalWidth()
    {
        return mTotalWidth;
    }
}
